<!DOCTYPE html>
<html>
<head>
    <title>药品查询</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/style.css"/>
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        $(function () {
            $('#newNav').click(function () {
                window.location.href = "add.html?pageSize=" + globalPageSize + "&pageNum=" + globalPageNum;
            });
            var indexUrl = decodeURI(document.location.toString());
            let pageNum = indexUrl.GetValue("pageNum");
            let drName = indexUrl.GetValue("drName");
            let drType = indexUrl.GetValue("drType");
            // 默认进来的处理
            if (pageNum == null) {
                queryList(1);
            } else {
                // 从编辑页面或者look页面返回的时候
                $("#drName").val(drName);
                $("#drType").val(drType);
                queryList(pageNum);
            }
            // 监听查询
            $("#query").on("click", function(){
                queryList(globalPageNum);
            })
            // 监听清空
            $("#reset").on("click", function(){
                $("#drName").val('');
                $("#drType").val('')
                queryList(1);
            })
        });

        // 重写queryList(分页查询方法)
        queryList = function (pageNum) {
            // 查询的条件
            let drName = $("#drName").val();
            let drType = $("#drType").val();
            let reqData = {
                pageNum: pageNum,
                pageSize: globalPageSize,
                drName: drName,
                drType: drType,
                jwtToken: jwtToken
            };
            layer.load(0)
            $.ajax({
                type: "GET",
                url: origin + "/drug/list",
				xhrFields: {
				   withCredentials: true
				},
				crossDomain: true,
                data: reqData,
                success: function (data) {
                    layer.closeAll()
                    if (data.code === 200) {
                        // 更新全局分页参数
                        globalPageSize = data.obj.pageSize;
                        globalPageNum = data.obj.pageNum;
                        // 填充分页
                        fillPageData(data.obj);
                        // 填充内容
                        fillContent(data.obj);
                        $("#checkall").removeAttr("checked");
                    } else {
                        layer.msg(data.msg, function () {
                        });
                    }
                }
                // 后端拦截ajax请求进行登陆跳转需要前端相互配合, 每一个ajax请求都需要加上以下的代码
                , complete: function (xhr, status) {
                    myComplete(xhr, status);
                }
            });
        }

        // 填充列表数据
        function fillContent(pageInfo) {
            $("#mycon").find("tbody").empty();
            // 查询的条件
            let drName = $("#drName").val();
            let drType = $("#drType").val();
            $.each(pageInfo.list, function () {
                let state = this.drIsDel == 0 ? '销售中' : '已下架';
                let editUrl = 'edit.html?pageNum=' + pageInfo.pageNum + '&pageSize=' + pageInfo.pageSize + '&drId=' + this.drId + '&drName=' + drName + '&drType=' + drType;
                let lookUrl = 'look.html?pageNum=' + pageInfo.pageNum + '&pageSize=' + pageInfo.pageSize + '&drId=' + this.drId + '&drName=' + drName + '&drType=' + drType;
                let add_medicineUrl = 'add_medicine.html?pageNum=' + pageInfo.pageNum + '&pageSize=' + pageInfo.pageSize + '&drId=' + this.drId + '&drName=' + drName + '&drType=' + drType;
                let con = "<tr>\n" +
                    "             <td style='vertical-align:middle;'><input type='checkbox' name='check' value='" + this.drId + "'></td>\n" +
                    "             <td>" + this.drId + "</td>\n" +
                    "             <td>" + this.drName + "</td>\n" +
                    "             <td>" + this.drType + "</td>\n" +
                    "             <td>" + this.drSimDesc + "</td>\n" +
                    "             <td>" + state + "</td>\n" +
                    "             <td>" + this.drNumber + "</td>\n" +
                    "             <td>\n" +
                    "                 <a href='" + editUrl + "'>更改</a>&nbsp;&nbsp;&nbsp;"
                    + "<a href='" + add_medicineUrl + "'>添加库存</a>&nbsp;&nbsp;&nbsp;"
                    + "<a href='" + lookUrl + "'>详情</a>\n" +
                    "             </td>\n" +
                    "         </tr>"
                $("#mycon").find("tbody").append($(con))
            })
        }

        function checkall() {
            var alls = document.getElementsByName("check");
            var ch = document.getElementById("checkall");
            if (ch.checked) {
                for (var i = 0; i < alls.length; i++) {
                    alls[i].checked = true;
                }
            } else {
                for (var i = 0; i < alls.length; i++) {
                    alls[i].checked = false;
                }
            }
        }
    </script>
</head>
<body>

<div class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">药品名称：</td>
            <td><input type="text" name="drName" id="drName" value=""/></td>
            <td width="10%" class="tableleft">药品类型：</td>
            <td><select name="drType" id="drType">
                <option value="">未选择</option>
                <option value="中药">中药</option>
                <option value="西药">西药</option>
                <option value="处方">处方</option>
            </select></td>
        </tr>
        <tr>
            <td colspan="4">
                <center>
                    <button type="button" id="query" class="btn btn-primary">查询</button>
                    <button type="reset" id="reset" class="btn btn-primary">清空</button>
                </center>
            </td>
        </tr>
    </table>
</div>

<table id="mycon" class="table table-bordered table-hover definewidth m10">
    <thead>
    <tr>
        <th><input type="checkbox" id="checkall" onChange="checkall();"></th>
        <th>药品编号</th>
        <th>药品名称</th>
        <th>药品类型</th>
        <th>简单描述</th>
        <th>状态</th>
        <th>剩余量</th>
        <th>操作</th>
    </tr>
    </thead>
    <!-- 列表数据 -->
    <tbody>
    </tbody>
</table>

<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <th colspan="5">
            <div id="pageInfo-box" class="inline pull-right page">
            </div>
            <div>
                <button type="button" class="btn btn-success" id="newNav">添加新药</button>
                <button class="btn btn-success" id="exportByDrIds">导出Excel（选中）</button>
                <button class="btn btn-success" id="exportExcel">导出Excel（所有数据）</button>
            </div>

        </th>
    </tr>
</table>
<script>
    $(function () {
        // 导出选中的数据
        $("#exportByDrIds").click(function () {
            // 获取复选框集合
            let allCheck = document.getElementsByName("check");
            // 获取导出数据的主键
            let drIds = [];
            for (let i = 0; i < allCheck.length; i++) {
                if (allCheck[i].checked) {
                    // 将主键拼接，作为地址参数，以集合的形式
                    drIds.push(allCheck[i].value);
                }
            }
            if (drIds.length == 0) {
                layer.msg("没有选中数据，不能导出!");
            } else {
                let url = origin + '/drug/export/drIds';
                reqExport(url, "药品信息", drIds, "drId");
                // 清除复选框
                $("input[name='check']:checked").prop("checked", false);
            }
        });

        // 请求导出全部的数据，可以设置查询条件，需要拼接到地址后面
        $("#exportExcel").click(function () {
            // 查询的条件
            let drName = $("#drName").val();
            let drType = $("#drType").val();
            let url = origin + '/drug/export?drName=' + drName + "&drType=" + drType + "&jwtToken=" + jwtToken;
            reqExport(url, "药品信息", null, "");
        });
    })
</script>

</body>
</html>
